package edu.cs.usask.ca.silver.plugin.rcp.views.projectviewer;

import java.util.Hashtable;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.ids.BaseId;
import edu.cs.usask.ca.silver.plugin.rcp.app.Application;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverEntitiesIncludingIconsEnumerations;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverTreeObject;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

public class ProjectViewLabelProvider implements ILabelProvider {

	private final Hashtable<String, Image> icons;
	private final StorageI storage;
	private static Logger logger = Logger.getLogger(ProjectViewLabelProvider.class);

	/**
	 * Class constructor initialises the icon cache
	 */
	public ProjectViewLabelProvider(StorageI storage) {

		this.storage = storage;
		icons = new Hashtable<String, Image>();
		
		for (SilverEntitiesIncludingIconsEnumerations enumId : SilverEntitiesIncludingIconsEnumerations
				.values()) {
			icons.put(
					enumId.name(),
					AbstractUIPlugin.imageDescriptorFromPlugin(Application.ID,
							enumId.getIconPath()).createImage());
		}
	}

	/**
	 * Provides an image label for this TreeObject
	 * 
	 * @param element
	 *            a TreeObject
	 * @return an image label for this TreeObject
	 */
	public Image getImage(Object element) {
		assert (element instanceof SilverTreeObject);
		// extracts the type of this TreeObject
		// String type = ((SilverTreeObject)element).getSilverComponentType();;
		BaseId objectID = ((SilverTreeObject) element).getSilverObjectId();
		// root element has no image label
		try {
			return icons.get(SilverEntitiesIncludingIconsEnumerations
					.getEnumIdNameBasedOnBaseIdClassName(objectID));
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"error in ProjectViewProvider", e);
		}
		return null;
	}

	/**
	 * Provides a text label for this TreeObject
	 * 
	 * @param element
	 *            a TreeObject
	 * @return a text label for this TreeObject
	 */
	public String getText(Object element) {

		assert (element instanceof SilverTreeObject);

		String objectName = "";
		try {
			objectName = ((SilverTreeObject) element).getSilverObjectId()
					.getEntryNameFromDatabase(storage);
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"error in ProjectViewProvider", e);
		}

		return objectName;
	}

	/**
	 * disposes of the images in the iconcache
	 */
	public void dispose() {

		// iterates through all the images in the cache and disposes them

		for (Iterator<Image> i = icons.values().iterator(); i.hasNext();) {
			((Image) i.next()).dispose();
		}
		icons.clear();

	}

	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	public void addListener(ILabelProviderListener listener) {
	}

	public void removeListener(ILabelProviderListener listener) {
	}

}
